package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.UnloadPlace_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class UnloadPlaceController extends Controller {

    private final FormFactory formFactory;
    private final UnloadPlace_Repository unloadPlace_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public UnloadPlaceController(FormFactory formFactory, UnloadPlace_Repository unloadPlace_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.unloadPlace_Repository = unloadPlace_Repository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> GetUnloadPlaceByDeptID() {
		JsonNode json = request().body().asJson();
		String DeptID = json.findPath("DeptID").textValue();
    	
	    return unloadPlace_Repository.GetUnloadPlaceByDeptID(DeptID).thenApplyAsync(_GetListByFunc -> {
    		return ok(Json.toJson(_GetListByFunc));
	    }, ec.current());
    }

    public CompletionStage<Result> ConfirmStatus() {
        JsonNode json = request().body().asJson();

        return unloadPlace_Repository.ConfirmStatus().thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
        }, ec.current());
    }

    public CompletionStage<Result> UserConfirmUnloadPlace() {
        JsonNode json = request().body().asJson();
        String ApplyID = json.findPath("ApplyID").textValue();
        String UnloadPlace = json.findPath("UnloadPlace").textValue();


        return unloadPlace_Repository.UserConfirmUnloadPlace(ApplyID,UnloadPlace).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
        }, ec.current());
    }

    public CompletionStage<Result> getGPSUnloadPlace() {
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        Double Lat = json.findPath("Lat").doubleValue();
        Double Lng = json.findPath("Lng").doubleValue();

//        Document doc_UnloadPlace = request().body().asXml();
//        //将Node转换为Element，然后取Attribute
//        String PhoneNumber = ((Element)(XPath.selectNode("getGPSUnloadPlace",doc_UnloadPlace))).getAttribute("PhoneNumber");
//        Double Lat = ((Element)(XPath.selectNode("getGPSUnloadPlace",doc_UnloadPlace))).getAttribute("Lat");
//        Double Lng = ((Element)(XPath.selectNode("getGPSUnloadPlace",doc_UnloadPlace))).getAttribute("Lng");

        return unloadPlace_Repository.getGPSUnloadPlace(PhoneNumber, Lat, Lng).thenApplyAsync(_GetListByFunc -> {
//            return ok(Json.toJson(_GetListByFunc));
            return ok(_GetListByFunc.getXmlString()).as("application/xml");
        }, ec.current());
    }

    public CompletionStage<Result> saveCoordinates() {
        JsonNode json = request().body().asJson();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String UnloadPlaceName = json.findPath("UnloadPlaceName").textValue();
        Double Lat = json.findPath("Lat").doubleValue();
        Double Lng = json.findPath("Lng").doubleValue();
        BigDecimal mileage = json.findPath("mileage").decimalValue();

        return unloadPlace_Repository.saveCoordinates(PhoneNumber, UnloadPlaceName,Lat, Lng, mileage).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
//            return ok(_GetListByFunc.getXmlString()).as("application/xml");
        }, ec.current());
    }

    public CompletionStage<Result> getUnloadPlaceByApplyId() {
        JsonNode json = request().body().asJson();
        Integer applyId = json.findPath("applyId").intValue();

        return unloadPlace_Repository.getUnloadPlaceByApplyId(applyId).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
//            return ok(_GetListByFunc.getXmlString()).as("application/xml");
        }, ec.current());
    }

    public CompletionStage<Result> getUnloadPlaceByPoundNo() {
        JsonNode json = request().body().asJson();
        String poundNo = json.findPath("poundNo").textValue();

        return unloadPlace_Repository.getUnloadPlaceByPoundNo(poundNo).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
//            return ok(_GetListByFunc.getXmlString()).as("application/xml");
        }, ec.current());
    }
}
